package com.youlu.campus.service.ranking.impl;

import com.youlu.campus.entity.UserRankBlackList;
import com.youlu.campus.service.ranking.UserRankBlackListService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserRankBlackListServiceImpl implements UserRankBlackListService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public UserRankBlackList find(String userId, String activityId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId));
        return mongoTemplate.findOne(query, UserRankBlackList.class);
    }

    @Override
    public void create(UserRankBlackList req) {
        req.setId(null);
        req.setCreatedTime(new Date());
        mongoTemplate.insert(req);
    }

    @Override
    public void delete(String userId, String activityId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId));
        mongoTemplate.remove(query, UserRankBlackList.class);
    }

    @Override
    public List<UserRankBlackList> find(String activityId) {
        Query query = new Query().addCriteria(Criteria.where("activityId").is(activityId));
        return mongoTemplate.find(query, UserRankBlackList.class);
    }

    @Override
    public List<String> findUserIds(String activityId) {
        List<UserRankBlackList> datas = this.find(activityId);
        if (CollectionUtils.isEmpty(datas)) {
            return new ArrayList<>();
        }
        List<String> uids = new ArrayList<>();
        for (UserRankBlackList u : datas) {
            uids.add(u.getUserId());
        }
        return uids;
    }

    @Override
    public boolean exist(String userId, String activityId) {
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("activityId").is(activityId));
        return mongoTemplate.exists(query, UserRankBlackList.class);
    }
}
